import router from '../router'
import store from '../store'

import { Message } from 'element-ui'
// 导航守卫的封装
export default {
  install: (Vue) => {
    router.beforeEach((to, from, next) => {
      if (store.state.user.token) {
        if (to.path.includes('/login')) {
          next(false)
        } else {
          next()
        }
      } else {
        // 未登录不允许调到支付和个人中心页面
        if (
          to.path.includes('/trade') ||
          to.path.includes('/center') ||
          to.path.includes('/pay')
        ) {
          Message.error('您还未登录,请先进行登录')
          next(`/login/?redirect=${to.path}`)
        } else {
          next()
        }
      }
    })
  }
}
